//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.ub.common.commons;

import com.github.knightliao.apollo.db.bo.BaseObject;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ThreadContext {
    private static final ThreadLocal<Map<String, Object>> CTX_HOLDER = new ThreadLocal();
    private static final String SHARD_KEY = "shardKey";
    private static final String VISITOR_KEY = "sessionVisitor";
    private static final String THREAD_LOG_KEY = "threadLog";
    private static final String SESSION_KEY = "sessionId";

    static {
        CTX_HOLDER.set(new HashMap());
    }

    public ThreadContext() {
    }

    public static final void putContext(String key, Object value) {
        Map<String, Object> ctx = CTX_HOLDER.get();
        if (null == ctx) {
            ctx = new HashMap<String, Object>();
            CTX_HOLDER.set(ctx);
        }

        ctx.put(key, value);
    }

    public static final <T> T getContext(String key) {
        Map ctx = (Map)CTX_HOLDER.get();
        return ctx == null?null:(T)ctx.get(key);
    }

    public static final Map<String, Object> getContext() {
        Map ctx = (Map)CTX_HOLDER.get();
        return ctx == null?null:ctx;
    }

    public static final void remove(String key) {
        Map ctx = (Map)CTX_HOLDER.get();
        if(ctx != null) {
            ctx.remove(key);
        }

    }

    public static final boolean contains(String key) {
        Map ctx = (Map)CTX_HOLDER.get();
        return ctx != null?ctx.containsKey(key):false;
    }

    public static final void clean() {
        CTX_HOLDER.set(null);
    }

    public static final void init() {
        CTX_HOLDER.set(new HashMap());
    }

    public static final <K extends Serializable> K getShardKey() {
        return (K)getContext("shardKey");
    }

    public static final <K extends Serializable> void putShardKey(K shardKey) {
        putContext("shardKey", shardKey);
    }

    public static final <U extends BaseObject<?>> U getSessionVisitor() {
        return (U)getContext("sessionVisitor");
    }

    public static final <K extends Serializable, U extends BaseObject<K>> void putSessionVisitor(U sessionVisitor) {
        putContext("sessionVisitor", sessionVisitor);
    }

    public static final void putThreadLog(Integer logLevel) {
        putContext("threadLog", logLevel);
    }

    public static final Integer getThreadLog() {
        return (Integer)getContext("threadLog");
    }

    public static final void putSessionId(String sessionId) {
        putContext("sessionId", sessionId);
    }

    public static final String getSessionId() {
        return (String)getContext("sessionId");
    }

    public static final void removeSessionId() {
        remove("sessionId");
    }
}
